home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / HARDWARE.SWG / 0013_CPU Info.pas < prev    next >
Pascal/Delphi Source File  |  1993-10-28  |  11KB  |  239 lines

  1. Unit CPUInfo;
  2.  
  3. {        Version 1.1.0.P
  4.  
  5.         Requires Borland Turbo Pascal version 6.0 or later to compile.
  6.         Author:  Bruce J. Lackore.  Created Saturday, October 9, 1993.
  7. }
  8.  
  9. {$IFDEF Test}
  10.         {$A+,B-,D+,F-,G-,I+,L+,O-,R+,S+,V-,X+}
  11. {$ELSE}
  12.         {$A+,B-,D-,F-,G-,I-,L-,O-,R-,S-,V-,X+}
  13. {$ENDIF}
  14.  
  15. {        This unit contains a handy gadget for determining the CPU speed.  It is NOT
  16.         coded for the Pentium family (if anyone wants to take a shot at it, please
  17.         let me know the results)!
  18. }
  19.  
  20. Interface
  21.  
  22. Const
  23.         Cpu8086                                                                          = 1;
  24.         Cpu80286                                                                         = 2;
  25.         Cpu80386                                                                         = 3;
  26.         Cpu80486                                                                         = 4;
  27.  
  28. Function WhatCPU:  Word;
  29.  
  30. {        This function examines the CPU and returns a number corresponding to the
  31.         CPU type;  1 for 8086, 3 for 80386, etc.  This procedure came right out of
  32.         Neil Rubenking's Turbo Pascal 6.0 Techniques and Utilities (thanx Neil!).
  33. }
  34.  
  35. Procedure CPUSpd(Var MHz, KHz:  Word);
  36.  
  37. {        This procedure is a ROUGH estimation of how fast the CPU is running in
  38.         MegaHertz.  It was adapted from a C program found in the Intel forum of
  39.         CIS written by Glenn Dill.  I had to do some finagling of the original code
  40.         because C allows for a 32-bit UNSIGNED integer, whereas Pascal allows for a
  41.         32-bit SIGNED integer (the LongInt), therefore, I was forced to reduce all
  42.         calculations by 10 in order to get it to fit properly.
  43. }
  44.  
  45. { ************************************************************************** }
  46.  
  47. Implementation
  48.  
  49. Function WhatCPU;  Assembler;
  50.  
  51.         Asm  { Function WhatCPU }
  52.                         MOV            DX,Cpu8086
  53.                         PUSH           SP
  54.                         POP            AX
  55.                         CMP            SP,AX
  56.                         JNE                 @OUT
  57.                         MOV                 DX,Cpu80286
  58.                         PUSHF
  59.                         POP            AX
  60.                         OR             AX,4000h
  61.                         PUSH           AX
  62.                         POPF
  63.                         PUSHF
  64.                         POP            AX
  65.                         TEST           AX,4000h
  66.                         JE             @OUT
  67.                         MOV                 DX,Cpu80386                        {        "DB 66h" makes '386 extended instruction }
  68.                         DB 66h; MOV BX,SP              {        MOV EBX,ESP }
  69.                         DB 66h, 83h, 0E4h, 0FCh {        AND ESP,FFFC        }
  70.                         DB 66h; PUSHF           {        PUSHFD }
  71.                         DB 66h; POP AX          {        POP EAX        }
  72.                         DB 66h; MOV CX, AX      {        MOV ECX,EAX }
  73.                         DB 66h, 35h, 00h
  74.                         DB 00h, 04h, 00         {        XOR EAX,00040000        }
  75.                         DB 66h; PUSH AX         {        PUSH EAX }
  76.                         DB 66h; POPF            {        POPFD }
  77.                         DB 66h; PUSHF           {        PUSHFD }
  78.                         DB 66h; POP AX          {        POP EAX }
  79.                         DB 66h, 25h,00h
  80.                         DB 00h, 04h,00h                {        AND EAX,00040000 }
  81.                         DB 66h, 81h,0E1h,00h
  82.                         DB 00h, 04h,00h                {        AND ECX,00040000 }
  83.                         DB 66h; CMP AX,CX              {        CMP EAX,ECX }
  84.                         JE @Not486
  85.                         MOV DX, Cpu80486
  86.                 @Not486:
  87.                         DB 66h; PUSH CX         {        PUSH ECX }
  88.                         DB 66h; POPF            {        POPFD }
  89.                         DB 66h; MOV SP, BX      {        MOV ESP,EBX }
  90.                 @Out:
  91.                         MOV AX, DX
  92.         End;        { Function WhatCPU }
  93.  
  94. Procedure CPUSpd;
  95.  
  96.         Const
  97.                 Processor_cycles:                                Array[0..4] of Byte = (165, 165, 25, 103, 42);
  98.                                                                                                                 {        Cycle times of 8086, 80186, 80286, 80386, 80486}
  99.  
  100.                 {        Notice that here I have defined the 8086 as a Processor type of 0 vice
  101.                         the returned value of 1 from WhatCPU.  Since the original code did not
  102.                         distinguish between the 8086 and the 80186, I can get away with this.
  103.                 }
  104.  
  105.         Var
  106.                 Ticks,
  107.                 Cycles,
  108.                 CPS:                                                                                LongInt;
  109.                 Which_CPU:                                                        Word;
  110.  
  111.         Function i86_to_i286:  Word;  Assembler;
  112.  
  113.                 Asm  { Function i86_to_i286 }
  114.                         CLI
  115.                         MOV                CX,1234
  116.                         XOR                DX,DX
  117.                         XOR                AX,AX
  118.                         MOV                AL,$B8
  119.                         OUT                $43,AL
  120.                         IN                AL,$61
  121.                         OR                AL,1
  122.                         OUT                $61,AL
  123.                         XOR                AL,AL
  124.                         OUT                $42,AL
  125.                         OUT                $42,AL
  126.                         XOR                AX,AX
  127.                         IDIV        CX
  128.                         IDIV        CX
  129.                         IDIV        CX
  130.                         IDIV        CX
  131.                         IDIV        CX
  132.                         IDIV        CX
  133.                         IDIV        CX
  134.                         IDIV        CX
  135.                         IDIV        CX
  136.                         IDIV        CX
  137.                         IDIV        CX
  138.                         IDIV        CX
  139.                         IDIV        CX
  140.                         IDIV        CX
  141.                         IDIV        CX
  142.                         IDIV        CX
  143.                         IDIV        CX
  144.                         IDIV        CX
  145.                         IDIV        CX
  146.                         IDIV        CX
  147.                         IN                AL,$42
  148.                         MOV                AH,AL
  149.                         IN                AL,$42
  150.                         XCHG        AL,AH
  151.                         NEG                AX
  152.                         STI
  153.                 End;  { Function i86_to_i286 }
  154.  
  155.         Function i386_to_i486:  Word;        Assembler;
  156.  
  157.                 Asm  { Function i386_to_i486 }
  158.                         CLI
  159.                         MOV                AL,$B8
  160.                         OUT                $43,AL
  161.                         IN                AL,$61
  162.                         OR                AL,1
  163.                         OUT                $61,AL
  164.                         XOR                AL,AL
  165.                         OUT                $42,AL
  166.                         OUT                $42,AL
  167.                         DB 66H,$B8,00h,00h,00h,80h;
  168.                         DB 66H,0FH,$BC,$C8;                                {        BSF        ECX,EAX }
  169.                         DB 66H,0FH,$BC,$C8;                                {        BSF        ECX,EAX }
  170.                         DB 66H,0FH,$BC,$C8;                                {        BSF        ECX,EAX }
  171.                         DB 66H,0FH,$BC,$C8;                                {        BSF        ECX,EAX }
  172.                         DB 66H,0FH,$BC,$C8;                                {        BSF        ECX,EAX }
  173.                         DB 66H,0FH,$BC,$C8;                                {        BSF        ECX,EAX }
  174.                         DB 66H,0FH,$BC,$C8;                                {        BSF        ECX,EAX }
  175.                         DB 66H,0FH,$BC,$C8;                                {        BSF        ECX,EAX }
  176.                         DB 66H,0FH,$BC,$C8;                                {        BSF        ECX,EAX }
  177.                         DB 66H,0FH,$BC,$C8;                                {        BSF        ECX,EAX }
  178.                         DB 66H,0FH,$BC,$C8;                                {        BSF        ECX,EAX }
  179.                         DB 66H,0FH,$BC,$C8;                                {        BSF        ECX,EAX }
  180.                         DB 66H,0FH,$BC,$C8;                                {        BSF        ECX,EAX }
  181.                         DB 66H,0FH,$BC,$C8;                                {        BSF        ECX,EAX }
  182.                         DB 66H,0FH,$BC,$C8;                                {        BSF        ECX,EAX }
  183.                         DB 66H,0FH,$BC,$C8;                                {        BSF        ECX,EAX }
  184.                         DB 66H,0FH,$BC,$C8;                                {        BSF        ECX,EAX }
  185.                         DB 66H,0FH,$BC,$C8;                                {        BSF        ECX,EAX }
  186.                         DB 66H,0FH,$BC,$C8;                                {        BSF        ECX,EAX }
  187.                         DB 66H,0FH,$BC,$C8;                                {        BSF        ECX,EAX }
  188.                         IN                AL,42H
  189.                         MOV   AH,AL
  190.                         IN                AL,42H
  191.                         XCHG        AL,AH
  192.                         NEG                AX
  193.                         STI
  194.                 End;  { Function i386_to_486 }
  195.  
  196.         Begin  { Procedure CPUSpd }
  197.                 Which_CPU := WhatCPU;
  198.                 If Which_cpu < 3 Then
  199.                         Ticks := i86_to_i286
  200.                 Else
  201.                         Ticks := i386_to_i486;
  202.                 Cycles := 20 * Processor_cycles[Which_CPU];
  203.                 CPS := (Cycles * 119318) Div Ticks;
  204.                 MHz := CPS Div 100000;
  205.                 KHz := (CPS Mod 100000 + 500) Div 1000
  206.         End;  { Procedure CPUSpd }
  207.  
  208. End.  { Unit CPUInfo }
  209.  
  210. { ---------------------   TEST PROGRAM --------------------------}
  211.  
  212. Program CPUSpeed;
  213.  
  214. {        Version 1.0.0.T.
  215.  
  216.         Requires Borland Turbo Pascal version 6.0 or later to compile.
  217.  
  218.         Author:  Bruce J. Lackore.  Created Saturday, October 9, 1993.
  219. }
  220.  
  221. {$IFDEF Test}
  222.         {$A+,B-,D+,E+,F-,G-,I+,L+,N-,R+,S+,V-,X+}
  223. {$ELSE}
  224.         {$A+,B-,D-,E+,F-,G-,I-,L-,N-,R-,S-,V-,X+}
  225. {$ENDIF}
  226.  
  227. {$M 1024, 0, 0}
  228.  
  229. Uses CPUInfo;
  230.  
  231. Var
  232.         MHz,
  233.         KHz:                                                                                        Word;
  234.  
  235. Begin  { Program: Cpuspeed }
  236.         CpuSpd(MHz, KHz);
  237.         Writeln('The CPU speed is ', MHz, '.', KHz, ' MHz.')
  238. End.  { Program:  Cpuspeed }
  239.